package cn.jbit.test.io;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Scanner;

/**
 * 源码学院只为培养优秀的bat程序员而生
 *
 * @author 学员张在成
 * @data 2021/6/27
 */
public class NioTestClient {

    private  static  volatile  boolean stop=false;

    public static void main(String[] args) throws IOException {

        SocketChannel socketChannel=SocketChannel.open();

        socketChannel.connect(new InetSocketAddress("localhost",8084));

        //socketChannel.bind(new InetSocketAddress(8084));

        socketChannel.configureBlocking(false);

        //客户端写数据
        while (!stop){

            Scanner sc=new Scanner(System.in);

            String next = sc.next();

            byte[] bytes=next.getBytes();

            //放到buffer缓存区
            ByteBuffer buffer1=ByteBuffer.allocate(bytes.length);

            buffer1.put(bytes);

            buffer1.flip();

            //socketChannel
            socketChannel.write(buffer1);

            System.out.println("发送数据到服务端");


        }
    }
}
